package functional

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 路径中直接 加上参数值
func GetUrlRule(ctx *gin.Context) {
	id := ctx.Param("id")

	//ctx.HTML(http.StatusOK, "GetHtml/index.html", nil)
	ctx.String(http.StatusOK, "hello,%s", id)
}

func GetUrlRule2(ctx *gin.Context) {
	id := ctx.Param("id")

	//ctx.HTML(http.StatusOK, "GetHtml/index.html", nil)
	ctx.String(http.StatusOK, "hello,%s", id)
}

func GetQueryData(ctx *gin.Context) {
	id := ctx.Query("id")

	name := ctx.DefaultQuery("name", "Jackson")
	fmt.Println(name)
	ctx.String(http.StatusOK, "Query,%s", id)
}

func GetQueryArrData(ctx *gin.Context) {
	ids := ctx.QueryArray("ids")

	ctx.String(http.StatusOK, "QueryArr,%s", ids)
}

func GetQueryMapData(ctx *gin.Context) {
	user := ctx.QueryMap("user")

	ctx.String(http.StatusOK, "QueryMap,%s", user)
}

// post 请求
func PostData(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "PostHtml/index.html", nil)
}

func DoUserAdd(ctx *gin.Context) {
	username := ctx.PostForm("username")
	password := ctx.PostForm("password")
	fmt.Println(username)
	fmt.Println(password)
	ctx.String(http.StatusOK, "添加成功")
}

func PostData2(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "PostHtml/item.html", nil)
}

func DoUserAdd2(ctx *gin.Context) {
	username := ctx.PostForm("username")
	password := ctx.PostForm("password")
	age := ctx.DefaultPostForm("age", "154")

	happy := ctx.PostFormArray("happy")

	user := ctx.PostFormMap("user")

	fmt.Println(username)
	fmt.Println(password)
	fmt.Println(age)
	fmt.Println(happy)
	fmt.Println(user)

	ctx.String(http.StatusOK, "添加成功")
}

func PostData3(ctx *gin.Context) {

	ctx.HTML(http.StatusOK, "PostHtml/item2.html", nil)
}

func DoUserAdd3(ctx *gin.Context) {
	username := ctx.PostForm("username")
	password := ctx.PostForm("password")
	age := ctx.PostForm("age")
	fmt.Println(username)
	fmt.Println(password)
	fmt.Println(age)

	//map_data := map[string]interface{}{
	//	"code": 200,
	//	"msg":  "成功",
	//}
	ctx.JSON(http.StatusOK, gin.H{"code": 200, "msg": "成功！"})
}
